package com.apache.common.plugins.flow;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.BusinessException;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 待办任务插件
 * @Author IUS platform administrators create generation
 * @Date 2018-11-16 14:05:35
 */
public class FlowSearchPluginImpl implements CustomMethodPlugin {
	private Logger log = Logger.getLogger(getClass());

	@Override
	public Object beforeInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件事前方法....");

		String sysName = String.valueOf(param.get("sysName"));
		String auditType =  String.valueOf(param.get("auditType"));

		if(Validator.isNull(auditType)){
			throw new BusinessException("auditType参数不能为空！");
		}

		Map<String, Object> whereMap = new HashMap<String, Object>();
		if(!Validator.isNull(sysName)){
			whereMap.put("sysAuditFlow.wi_sysName", sysName);
		}
		if(!"all".equals(auditType)){
			whereMap.put("sysAuditFlow.wi_auditType", auditType);
		}
		ResultEntity resultEntity = IusPluginUtil.getInstance().select(String.valueOf(param.get("datasource")),"count","s_sysAuditFlow",whereMap);
		int size = 0;
		if(!StrUtil.isEmpty(resultEntity.getEntity())){
			size = NumberUtils.getInt(String.valueOf(resultEntity.getEntity()),0);
		}
		//只有size大于0时才证明该请求正确
		if(size>0){
			if(!"all".equals(auditType)) {
				param.put("sysAudit.w_auditType", auditType);
			}
			param.put("sysAudit.w_auditStatus", "10");
		 }else{
			throw new BusinessException("非法请求！");
		}


		return null;
	}

	@Override
	public Object doInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件方法....");

		return null;
	}

	@Override
	public Object afterInvoke(Map<String, Object> param) {
		log.info("start->> 执行插件事后方法....");

		return null;
	}

	/**
	 * 规则校验统一入口
	 * @param paramsVo
	 */
	public void run(ParamsVo paramsVo) {
		beforeInvoke(paramsVo.getParams());
	}


}

